package com.kcsj.dao.User;

import com.kcsj.domain.user.Resource;
import com.kcsj.domain.user.RoleAssResource;
import com.kcsj.util.JdbcHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.TreeSet;

/**
 * @author 胡庆阳
 * 29th Nov. 2020
 */
public class RoleAssResourceDao {
    private static final RoleAssResourceDao ROLE_ASS_FIRST_MENU_DAO =
            new RoleAssResourceDao();

    private RoleAssResourceDao() {

    }

    public static RoleAssResourceDao getInstance() {
        return ROLE_ASS_FIRST_MENU_DAO;
    }
    //更新角色权限
    public void update(RoleAssResource roleAssResource, Connection conn) throws SQLException {
        Collection<Integer> resources = roleAssResource.getResourceId();
        delete(roleAssResource.getId(), conn);
        String s = "insert into RoleAssResource(role_id,resource_id) value (?,?)";
        PreparedStatement preparedStatement = conn.prepareStatement(s);
        for (Integer id : resources) {
            preparedStatement.setInt(1,roleAssResource.getId());
            preparedStatement.setInt(2,id);
            preparedStatement.addBatch();
        }
        preparedStatement.executeBatch();
        //关闭连接
        JdbcHelper.close(preparedStatement);
    }
    //根据role_id和resource_id查询表中是否存在改信息
    public boolean findByRoleAndResource(Integer role_id,Integer resource_id, Connection conn) throws SQLException {
        boolean has = false;
        //预编译方式进行查询
        String s = "select * from RoleAssResource where role_id = ? and resource_id = ?";
        PreparedStatement preparedStatement = conn.prepareStatement(s);
        preparedStatement.setInt(1,role_id);
        preparedStatement.setInt(2,resource_id);
        ResultSet resultSet = preparedStatement.executeQuery();
        //将查询到的数据转为resource对象
        while (resultSet.next()){
           has = true;
        }
        //关闭连接
        JdbcHelper.close(resultSet,preparedStatement);
        return has;
    }

    //根据角色查询拥有的菜单和权限
    public Collection<Resource> findRoleMenu(Integer role_id, Connection conn) throws SQLException {
        Collection<Resource> resources = new TreeSet<>();
        //预编译方式进行查询
        String s = "select * from RoleAssResource where role_id = ?";
        PreparedStatement preparedStatement = conn.prepareStatement(s);
        preparedStatement.setInt(1,role_id);
        ResultSet resultSet = preparedStatement.executeQuery();
        //将查询到的数据转为resource对象
        while (resultSet.next()){
            int resource_id = resultSet.getInt(2);
            Resource resource = ResourceDao.getInstance().findMenu(role_id,resource_id,conn);
            if(resource!=null){
                resources.add(resource);
            }
        }
        //关闭连接
        JdbcHelper.close(resultSet,preparedStatement);
        return resources;
    }
    //查询角色拥有权限的id
    public Collection<Integer> findRoleResourceAndAuthorityIds(Integer role_id, Connection conn) throws SQLException {
        Collection<Integer> authorityIds = new LinkedList<>();
        //预编译方式进行查询
        String s = "select * from RoleAssResource where  role_id = ?";
        PreparedStatement preparedStatement = conn.prepareStatement(s);
        preparedStatement.setInt(1,role_id);
        ResultSet resultSet = preparedStatement.executeQuery();
        //将查询到的数据转为resource对象
        while (resultSet.next()){
            int resource_id = resultSet.getInt(2);
            Boolean hasChild = ResourceDao.getInstance().hasChild(resource_id,conn);
            if(!hasChild){
                authorityIds.add(resource_id);
            }
        }
        //关闭连接
        JdbcHelper.close(resultSet,preparedStatement);
        return authorityIds;
    }


    //删除
    private void delete(Integer id, Connection conn) throws SQLException {
        //预编译方式进行删除数据
        String s = "delete from roleAssResource where role_id = ?";
        PreparedStatement preparedStatement = conn.prepareStatement(s);
        preparedStatement.setInt(1,id);
        //如果执行影响行数大于0，则增加成功
        int i = preparedStatement.executeUpdate();
        //关闭连接
        JdbcHelper.close(preparedStatement);
    }

}

